<?php

namespace App\Http\Controllers\Admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\AdminRule;
use App\Models\AdminGroup;

class RoleController extends Controller
{
    

	/**
     * 角色管理
     *
     * @return mixed
     */
    public function adminGroup()
    {
        $groups = AdminGroup::where('status','=',1)->get()->toArray();
        // dd($groups);
        return view('admin.role.adminGroup',['groups'=>$groups]);
    }
    public function addGroup(Request $request)
    {
        if($request->isMethod('post')){
            $title =  $request->title;
            $msg = $request->msg;
            $rulesArr = $request->rule_ids;
            $rules = implode(',',$rulesArr);
            // dd($request->all());
            $status = 1;
            $create_at = time();
            $params = compact('title','msg','status','rules','create_at');
            $row = AdminGroup::create($params);
            if ($row) {
                return 1;
            } else {
                return 0;
            }
        }
        $ruleM = new AdminRule();
        $groups = $ruleM->orderBy('id')->get()->toArray();
        // dd($groups);
        $rule_data = $ruleM->getTreeData($groups,'level','title');
        return view('admin.role.addGroup',['rule_data' => $rule_data]);
    }
    public function editGroup(Request $request)
    {
        if($request->isMethod('post')){
            $input = $request->except('_token');
            $input['rules'] = implode(',',$input['rule_ids']);
            $input['update_at'] = time();
            $post = AdminGroup::find($input['id']);
            if ($post->update($input)) {
                return 1;
            } else {
                return 0;
            }
        }
        $id = $request->id;
        $group_data = AdminGroup::where('id','=',$id)->first()->toArray();
        $group_data['rules'] = explode(',', $group_data['rules']);
        $ruleM = new AdminRule();
        $groups = $ruleM->orderBy('id')->get()->toArray();
        // dd($groups);
        $rule_data = $ruleM->getTreeData($groups,'level','title');
        return view('admin.role.editGroup',['rule_data' => $rule_data,'group_data' => $group_data]);
    }

    public function delGroup(Request $request)
    {
        $id = $request->id;
        $post = AdminGroup::find($id);
        $post->status = 0;
        $row = $post->save();
        if ($row) {
            $call = 1;
        } else {
            $call = 0;
        }
        return $call;
    }


	/**
     * 权限管理
     *
     * @return mixed
     */
    public function adminPermission()
    {
        $adminRuleM = new AdminRule();
        // $rules = AdminRule::where('status','=',1)->get()->toArray();
        $rules = $adminRuleM->AdminRuleTree();
        // dd($rules);
        return view('admin.role.adminPermission',['rules'=>$rules]);
    }
    /**
     * 权限编辑
     *
     * @return mixed
     */
    public function addPermission(Request $request)
    {
        if($request->isMethod('post')){
            // dd($request->all());
            $pid =  $request->ppid;
            $name = $request->name;
            $title = $request->title;
            // dd($name);
            $status = 1;
            $create_at = time();
            $params = compact('pid','name','title','status','create_at');
            $row = AdminRule::create($params);
            if ($row) {
                return 1;
            } else {
                return 0;
            }
        }
        $pid = $request->pid;
        // dd($pid);
        return view('admin.role.addPermission',['pid'=>$pid]);
    }
    public function editPermission(Request $request)
    {
        if($request->isMethod('post')){
            $input = $request->except('_token');
            $input['update_at'] = time();
            // dd($data);
            $post = AdminRule::find($input['id']);
            if ($post->update($input)) {
                return 1;
            } else {
                return 0;
            }
        }
        $id = $request->id;
        // dd($pid);
        $data = AdminRule::where('id','=',$id)->first()->toArray();
        return view('admin.role.editPermission',['data'=>$data]);
    }
    public function delPermission(Request $request)
    {
        $id = $request->id;
        $post = AdminRule::find($id);
        $post->status = 0;
        $row = $post->save();
        if ($row) {
            $call = 1;
        } else {
            $call = 0;
        }
        return $call;
    }
    /**
     * 菜单列表
     *
     * @return mixed
     */
    public function adminNavList()
    {
        
        return view('admin.role.adminNavList');
    }
    /**
     * 编辑菜单
     *
     * @return mixed
     */
    public function addNav()
    {
        
        return view('admin.role.addNav');
    }
    public function editNav()
    {
        
        return view('admin.role.editNav');
    }
    /**
     * 删除菜单
     *
     * @return mixed
     */
    public function delNav()
    {
        
        return view('admin.role.delNav');
    }
}
